package com.jkdz.spyke.widgetUIs {
	import spark.components.Button;
	import spark.components.Form;
	import spark.components.FormItem;
	import spark.components.Group;
	import spark.layouts.HorizontalAlign;
	import spark.layouts.HorizontalLayout;
	import spark.layouts.VerticalAlign;
	import spark.layouts.VerticalLayout;

	import com.jkdz.spyke.flexUI.helpers.Link;
	import com.jkdz.spyke.flexUI.helpers.SuperWindow;
	import com.jkdz.spyke.flexUI.main.Main;
	import com.jkdz.spyke.rs.SpykeURLDisplay;
	import com.jkdz.spyke.rs.SpykeURLDisplayListener;

	import mx.controls.TextInput;
	import mx.core.FlexGlobals;
	import mx.events.FlexEvent;

	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.system.System;
	
	public class SpykeURLDisplayUI extends SpykeWidgetUI implements SpykeURLDisplayListener {
		
		private var widget:SpykeURLDisplay;
		private var window:SuperWindow;
		
		public function SpykeURLDisplayUI(widget:SpykeURLDisplay) {
			this.widget = widget;
		}
		
		public function nameChanged(newName:String):void {
			window.title = newName;
		}
		
		override public function display(onComplete:Function):void {
			
			widget.getName(function(name:String):void {
				widget.getID(function(id:String):void {
					showUI(name, id, onComplete);
				});
			});
		}
		
		public function unPublished():void {
			window.close();
		}
		
		private function showUI(name:String, id:String, onComplete:Function):void {
			var url:String = Main.LOCATION+"?"+id;
			
			var hl:HorizontalLayout = new HorizontalLayout();
			hl.gap = 10;
			hl.verticalAlign = VerticalAlign.MIDDLE;
			hl.paddingBottom = hl.paddingTop = hl.paddingRight = hl.paddingLeft = 10;
			layout = hl;
			
			var label:TextInput = new TextInput();
			label.selectable = true;
			label.editable = false;
			label.text = url;
			label.addEventListener(FlexEvent.CREATION_COMPLETE, function(e:Event):void {
				label.width += 20;
			});
			label.addEventListener(MouseEvent.CLICK, function(e:MouseEvent):void {
				label.selectRange(0, label.text.length);
			});
			this.addElement(label);
			
			var copyLink:Link = new Link("Copy");
			copyLink.addEventListener(MouseEvent.CLICK, function(e:MouseEvent):void {
				System.setClipboard(url);
				focusManager.setFocus(label);
				label.selectRange(0, label.text.length);
			});
			copyLink.toolTip = "Copy Room URL to clipboard.";
			this.addElement(copyLink);
			
			var changeNameLink:Link = new Link("Settings");
			changeNameLink.addEventListener(MouseEvent.CLICK, function(e:MouseEvent):void {
				showChangeNameDialog();	
			});
			changeNameLink.toolTip = "Click here to edit Spyke Room settings.";
			this.addElement(changeNameLink);
			
			this.window = SuperWindow.display(FlexGlobals.topLevelApplication as Main, this, name, false, false, true, false, new Point(10, 10));
			
			widget.registerListener(this);

			onComplete();
		}
		
		private function showChangeNameDialog():void {
			var crfg:Group = new Group();
			var vl:VerticalLayout = new VerticalLayout();
			vl.horizontalAlign = HorizontalAlign.CENTER;
			vl.paddingBottom = 10.0;
			crfg.layout = vl;
			var crff:Form = new Form();
			var crffi:FormItem = new FormItem();
			var crfti:TextInput = new TextInput();
			var crfb:Button = new Button();
			crfb.label = "OK";
			var submit:Function = function():void {
				if (crfti.text.length == 0 || rrfti.text.length == 0) {
					SuperWindow.alert(FlexGlobals.topLevelApplication as Main, "Wrong name", "The name cannot be empty.", function():void {
						cnw.close();
					});
				} else {
					(Main)(FlexGlobals.topLevelApplication).setUserName(crfti.text);
					widget.renameRoom(rrfti.text);
					cnw.close();
				}
			};
			crfb.addEventListener(MouseEvent.CLICK, function(e:MouseEvent):void {
				submit();
			});
			crffi.label = "Your name:";
			crffi.addElement(crfti);
			crfti.text = (Main)(FlexGlobals.topLevelApplication).getUserName();
			crfti.addEventListener(KeyboardEvent.KEY_DOWN, function(e:KeyboardEvent):void {
				if (e.charCode == 13) {
					submit();
				}
			});
			crfg.addElement(crff);
			
			var rrffi:FormItem = new FormItem();
			var rrfti:TextInput = new TextInput();
			rrffi.label = "Room name:";
			rrffi.addElement(rrfti);
			crff.addElement(rrffi);
			crff.addElement(crffi);
			rrfti.text = window.title;
			rrfti.addEventListener(KeyboardEvent.KEY_DOWN, function(e:KeyboardEvent):void {
				if (e.charCode == 13) {
					submit();
				}
			});
			
			crfg.addElement(crfb);
			var cnw:SuperWindow = SuperWindow.display((Main)(FlexGlobals.topLevelApplication), crfg, "SpykeRoom settings", true, true, true, false);
			focusManager.setFocus(crfti);
		}

	}
}